package com.dxy.app.widget.shrink_upvote;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import java.util.ArrayList;
import java.util.List;

/**
  *Created by duxingyu on 2017/7/20.18:08
  *Email:
  *Description:一般般表情
  **/

public class UpvoteJustView extends ShrinkUpvoteView {

  /**
   * 特色画笔
   * */
  private Paint facePaint;
  /**
   * 轨迹列表
   * */
  private List<Float> list;
  /**
   * 表情初始偏移量
   * */
  private float emojDelY=0;
  /**
   * 绘制咧嘴笑
   * */
  private float smileDelX;

  public UpvoteJustView(Context context) {
    super(context);
  }

  public UpvoteJustView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
  }

  public UpvoteJustView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  @Override protected void initSelfParams() {
    facePaint =new Paint();
    facePaint.setAntiAlias(true);
    facePaint.setStrokeWidth(eyeRadius/2);
    facePaint.setColor(colorBorder);
    facePaint.setStyle(Paint.Style.STROKE);
    facePaint.setStrokeCap(Paint.Cap.ROUND);

    list =new ArrayList<>();
    list.add(eyeToMiddleX);
    list.add(0-eyeToMiddleX);
    list.add(eyeToMiddleX);
    list.add(0-eyeToMiddleX);
    list.add(eyeToMiddleX);
    list.add(0-eyeToMiddleX);

    type =VoteType.JUST;
  }

  @Override protected void drawFace(Canvas canvas,float offsetY) {
    float eyeDelY =positionY-radius -eyeToTopY- offsetY-emojDelY;

    paint.setColor(colorBorder);
    canvas.drawCircle(positionX-eyeToMiddleX,eyeDelY,eyeRadius,paint);
    canvas.drawCircle(positionX+eyeToMiddleX,eyeDelY,eyeRadius,paint);

    facePaint.setColor(colorBorder);
    float mouthDelY =positionY-radius-offsetY-emojDelY;
    RectF rectF =new RectF(positionX-eyeToMiddleX-smileDelX,mouthDelY-smileDelX,positionX+eyeToMiddleX+smileDelX,mouthDelY+eyeToTopY*2+smileDelX*2);
    canvas.drawArc(rectF,30,120,false,facePaint);
  }

  @Override protected void shrinkControl() {
    for(float each:list){
      if(emojDelY<each){//上升
        for(float k=emojDelY;k<each;k++){
          emojDelY++;
          smileDelX+=1/2f;
          if(emojDelY>=each){
            emojDelY =each;
            break;
          }
          postInvalidate();
          try {
            Thread.sleep(SHRINK_TIME);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }else{//下降
        for(float k=emojDelY;k>0;k--){
          emojDelY --;
          smileDelX-=1/2f;
          if(emojDelY<=0){
            emojDelY =0;
            smileDelX =0;
            break;
          }
          postInvalidate();
          try {
            Thread.sleep(SHRINK_TIME);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    }
  }
}
